#include "menu.h"
#include <cstring>
#include <iostream>
#include <Windows.h>
using namespace std;


menu::menu(char *name, const char size, const char length)
{
	strcpy(title, name);
	itemsNumber = size;
	itemsLength = length;
	item = new char*[itemsNumber];
}

menu::~menu()
{
	for (int i = 0; i < itemsNumber; i++)
	{
		if (item[i]) delete[] item[i];
	}
	if (item) { delete[] item; }
}

void menu::addItems(char* name[])
{
	
	for (int i = 0; i < itemsNumber; i++)
	{
		item[i] = new char[itemsLength];
		strcpy(item[i], name[i]);
	}
}

void menu::showMenu(char x)
{
	const short itemColor = 0x70;
	const short menuColor = 0x37;
	system("color F0");
	//top-left border
	gotoxy(x, 0);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), menuColor);
	
	for (int j = 0; j < 3; j++)
	{
		for (int i = 0; i < 80; i++) cout << " ";
	}
	gotoxy(x,0);
	int menuWidth = 3;

	for (int i = 0; i < itemsNumber; i++) { menuWidth += strlen(item[i]) + 3; }

	cout << (char)218 << title;
	line(196, menuWidth - strlen(title) - 2);
	cout << (char)191;
	gotoxy(x, 1);
	cout <<(char)179<<" ";

	//items output
	char **curItem = item;
	while (curItem < item+itemsNumber)
	{
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), itemColor);
		cout << " " << *curItem << " ";
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), menuColor);
		cout << " ";
		curItem++;
	}

	//bottom-right border
	cout << (char)179;
	gotoxy(x, 2);
	cout << (char)192;
	line(196, menuWidth - 2);
	cout << (char)217;

	gotoxy(0, 4);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xF0);
}

void menu::gotoxy(short x, short y)
{
	COORD coord = { x, y };
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}


void menu::line(unsigned const char c, const int length)
{
	for (int i = 0; i < length; i++) {cout << c;}
}

